// pages/reservation/index.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    check: false,
    type: "网络咨询",
    selectTime: {},
    time: "",
    timeId: "",
    visitorList: [],
    visitorShow: false,
    visitorName: "",
    visitorId: 0,
    buttons: [
      {
        type: "default",
        className: "",
        text: "添加来访者信息",
        value: 0,
      },
      {
        type: "primary",
        extClass: "primary-butt",
        className: "",
        text: "确定",
        value: 1,
      },
    ],
    teacher: {},
    consultType: [],
    price: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    try {
      const res = await getApp().request({
        path: "/s/t/getTeacherById",
        method: "GET",
        data: {
          tid: "179132900338434048",
        },
      });
      const { teacher = {}, consultType = [] } = res;
      const price = consultType.find(
        (item) => item.type === this.data.type
      ).price;
      this.setData({
        teacher,
        consultType,
        price,
      });
    } catch {}
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.queryVisitorList();
  },
  async queryVisitorList() {
    try {
      const res = await getApp().request({
        path: "/s/v/listVisitors",
        method: "GET",
      });
      this.setData({
        visitorList: res,
      });
    } catch {}
  },
  // 打开预约时间弹窗
  handleOpen() {
    this.setData({
      show: true,
    });
  },
  // 重置选择的预约时间
  handleReset() {
    const child = this.selectComponent("#date-picker");
    child.handleClear();
    this.setData({
      selectTime: child.data.activeTimes,
      time: "",
    });
  },
  // 确认选择的预约时间
  handleSubmit() {
    const child = this.selectComponent("#date-picker");
    const slected = Object.keys(child.data.activeTimes);
    if (slected.length === 0) {
      wx.showToast({
        title: "请选择咨询时间",
        icon: "none",
      });
    } else {
      const [key] = Object.keys(child.data.activeTimes);
      const [value]: any = Object.values(child.data.activeTimes);
      this.setData({
        selectTime: child.data.activeTimes,
        time: `${key} ${value.time}`,
        timeId: value.id,
        show: false,
      });
    }
    console.log(child.data.activeTimes, "eee");
  },
  // 选择协议
  handleChange() {
    this.setData({
      check: !this.data.check,
    });
  },
  // 打开来访者
  async handleVisitor() {
    this.setData({
      visitorShow: true,
    });
  },
  bindbuttontap(e) {
    const index = e.detail.index;
    if (index === 0) {
      wx.navigateTo({
        url: "/pages/visitorInformation/index",
      });
    } else {
      this.setData({
        visitorShow: false,
      });
    }
  },
  // 选择来访者
  handleVisitorSelect(e) {
    const { name, id } = e.target.dataset;
    this.setData({
      visitorName: name,
      visitorId: id,
    });
  },
  handleType(e) {
    const { type } = e.target.dataset;
    const { consultType }: any = this.data;
    const price = consultType.find((item) => item.type === type).price;
    this.setData({
      type,
      price,
    });
  },
  // 支付
  async handlePayMent() {
    const {
      visitorName,
      visitorId,
      time,
      timeId,
      check,
      type,
      consultType = [],
    }: any = this.data;
    if (!visitorName) {
      wx.showToast({
        title: "请选择来访者",
        icon: "none",
      });
      return;
    }
    if (!time) {
      wx.showToast({
        title: "请选择预约时间",
        icon: "none",
      });
      return;
    }
    if (!check) {
      wx.showToast({
        title: "请阅读并同意预约须知",
        icon: "none",
      });
      return;
    }
    console.log(visitorId, type, time, timeId, "time");
    try {
      const consultTypeId = consultType.find((item) => item.type === type).id;
      const res = await getApp().request({
        path: "/s/o/addOrder",
        method: "POST",
        data: {
          consultTypeId,
          timeId: timeId,
          visitorId: visitorId,
          tid: "179132900338434048",
        },
      });
      wx.navigateTo({
        url: `/pages/orderDetails/index?orderId=${res}`,
      });
    } catch {}
  },
});
